/*
 * Created by JFormDesigner on Fri Jun 28 09:21:33 CST 2024
 */

package scct.zhurui.ui;

import scct.zhurui.dao.UserDao;
import scct.zhurui.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author zr155
 */
public class DlgUserupdate extends JDialog {
    int id;
    public DlgUserupdate(Window owner,int id) {
        super(owner);
        this.id=id;
        initView();
        initComponents();
    }

    private void initView() {
        UserDao userDao = new UserDao();

        User user = userDao.getUserById(id);

        txtName4.setText(user.getUsername());

        txtPass.setText(user.getPassword());
        txtAge2.setValue(user.getAge());
        textField11.setText(user.getMobile());
        if (user.getGender().equals("男")) {
            radMale.setSelected(true);
        } else {
            radFemale3.setSelected(true);
        }

    }
    private void radioButton1ComponentMoved(ComponentEvent e) {
        // TODO add your code here
    }

    private void btnAdd(ActionEvent e) {
        String username = txtName4.getText();
        String pass = txtPass.getText();
        String mobile = textField11.getText();
        int age = Integer.parseInt(txtAge2.getModel().getValue().toString());
        String gender = radMale.isSelected() ? "男" : "女";
        if (!username.matches("^\\w{3,10}$") || !pass.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的用户名/密码");
            return;
        }

        if (!mobile.matches("^1[3-9]\\d{9}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的手机号码");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        UserDao userDao = new UserDao();
        if (userDao.updateUser(id,pass,gender,age,mobile)){
            JOptionPane.showMessageDialog(this,"修改成功");
            dispose();
        }

    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhur
        this2 = new JDialog();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        txtName4 = new JTextField();
        txtPass = new JTextField();
        textField11 = new JTextField();
        txtAge2 = new JSpinner();
        radMale = new JRadioButton();
        radFemale3 = new JRadioButton();
        btnAdd = new JButton();
        btnCancel = new JButton();

        //======== this2 ========
        {
            this2.setTitle("\u4fee\u6539\u7528\u6237");
            this2.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentMoved(ComponentEvent e) {
                    radioButton1ComponentMoved(e);
                }
            });
            Container this2ContentPane = this2.getContentPane();
            this2ContentPane.setLayout(null);

            //---- label1 ----
            label1.setText("\u7528\u6237\u540d");
            this2ContentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(95, 29), label1.getPreferredSize()));

            //---- label2 ----
            label2.setText("\u5bc6\u7801");
            this2ContentPane.add(label2);
            label2.setBounds(95, 63, 35, 17);

            //---- label3 ----
            label3.setText("\u624b\u673a\u53f7");
            this2ContentPane.add(label3);
            label3.setBounds(95, 97, label3.getPreferredSize().width, 17);

            //---- label5 ----
            label5.setText("\u5e74\u9f84");
            this2ContentPane.add(label5);
            label5.setBounds(95, 131, label5.getPreferredSize().width, 17);

            //---- label6 ----
            label6.setText("\u6027\u522b");
            this2ContentPane.add(label6);
            label6.setBounds(95, 165, 33, 17);
            this2ContentPane.add(txtName4);
            txtName4.setBounds(145, 25, 145, 25);
            this2ContentPane.add(txtPass);
            txtPass.setBounds(145, 62, 145, 25);
            this2ContentPane.add(textField11);
            textField11.setBounds(145, 98, 145, 25);

            //---- txtAge2 ----
            txtAge2.setModel(new SpinnerNumberModel((byte) 0, (byte) 0, (byte) 127, (byte) 1));
            this2ContentPane.add(txtAge2);
            txtAge2.setBounds(145, 134, 145, 25);

            //---- radMale ----
            radMale.setText("\u7537");
            radMale.setSelected(true);
            radMale.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentMoved(ComponentEvent e) {
                    radioButton1ComponentMoved(e);
                }
            });
            this2ContentPane.add(radMale);
            radMale.setBounds(160, 165, 40, 16);

            //---- radFemale3 ----
            radFemale3.setText("\u5973");
            radFemale3.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentMoved(ComponentEvent e) {
                    radioButton1ComponentMoved(e);
                }
            });
            this2ContentPane.add(radFemale3);
            radFemale3.setBounds(220, 165, 40, 16);

            //---- btnAdd ----
            btnAdd.setText("\u4fee\u6539");
            btnAdd.addActionListener(e -> btnAdd(e));
            this2ContentPane.add(btnAdd);
            btnAdd.setBounds(new Rectangle(new Point(95, 205), btnAdd.getPreferredSize()));

            //---- btnCancel ----
            btnCancel.setText("\u53d6\u6d88");
            this2ContentPane.add(btnCancel);
            btnCancel.setBounds(new Rectangle(new Point(220, 205), btnCancel.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < this2ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = this2ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = this2ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                this2ContentPane.setMinimumSize(preferredSize);
                this2ContentPane.setPreferredSize(preferredSize);
            }
            this2.setSize(400, 300);
            this2.setLocationRelativeTo(this2.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhur
    private JDialog this2;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label5;
    private JLabel label6;
    private JTextField txtName4;
    private JTextField txtPass;
    private JTextField textField11;
    private JSpinner txtAge2;
    private JRadioButton radMale;
    private JRadioButton radFemale3;
    private JButton btnAdd;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
